package com.jiyinit.web.core.business.service;

import com.jiyinit.core.framework.base.paging.PagingResult;
import com.jiyinit.web.core.business.dao.TabCoreFunDao;
import com.jiyinit.web.core.business.dto.FunctionPageRequest;
import com.jiyinit.web.core.business.dto.FunctionRequest;
import com.jiyinit.web.core.business.entity.TabCoreFunEntity;
import com.jiyinit.web.core.business.enums.CommonEnum;
import com.jiyinit.web.core.business.vo.FunctionVo;
import com.jiyinit.web.core.common.util.UserUtil;
import com.jiyinit.web.core.common.vo.CurrentUserVo;
import io.netty.util.internal.chmv8.ConcurrentHashMapV8;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lr on 2017/12/20.
 */
@Service(value = "function.serviceImpl")
public class FunctionServiceImpl {

    @Resource(name = "core.tabCoreFunDao")
    private TabCoreFunDao tabCoreFunDao;

    public TabCoreFunEntity findByPK(Integer funId){
        return tabCoreFunDao.getFunctionById(funId);
    }

    public int update(FunctionRequest request){
        CurrentUserVo currentUser = UserUtil.getCurrentUser();
        Integer userId = currentUser.getUserId();

        TabCoreFunEntity function = new TabCoreFunEntity();
        function.setFunId(request.getFunId());
        function.setFunName(request.getFunName());
        function.setParentId(request.getParentId());
        function.setFunOrderBy(request.getFunOrderBy());
        function.setFunType(request.getFunType());
        function.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
        function.setLastModifyTime(new Date());
        function.setLastMdifyId(userId);
        return tabCoreFunDao.update(function);
    }

    public int add(FunctionRequest request){

        TabCoreFunEntity parent = tabCoreFunDao.getFunctionById(request.getParentId());

        CurrentUserVo currentUser = UserUtil.getCurrentUser();
        Integer userId = currentUser.getUserId();

        TabCoreFunEntity function = new TabCoreFunEntity();
        function.setFunName(request.getFunName());
        function.setParentId(request.getParentId());
        function.setFunOrderBy(request.getFunOrderBy());
        function.setFunLevel(parent.getFunLevel().intValue()+ 1);
        function.setFunType(request.getFunType());
        function.setIsDelete(CommonEnum.IsDelete.COMMON.getCode());
        function.setCreateId(userId);
        function.setCreateTime(new Date());
        return tabCoreFunDao.save(function);
    }

    public PagingResult findByPage(FunctionPageRequest functionPageRequest){
        return tabCoreFunDao.findByPage(functionPageRequest);
    }

    public PagingResult findPageByParentId(FunctionPageRequest functionPageRequest){
        return tabCoreFunDao.findPageByParentId(functionPageRequest);
    }

    public List<FunctionVo> selectByParentId(Integer parentId){
        return tabCoreFunDao.findByParentId(parentId);
    }

    public int delete(Integer[] funIds){
        List<TabCoreFunEntity> list = new ArrayList<>();
        for(Integer id : funIds) {
            TabCoreFunEntity entity = new TabCoreFunEntity();
            entity.setFunId(id);
            entity.setIsDelete(CommonEnum.IsDelete.DELETED.getCode());
            entity.setLastMdifyId(UserUtil.getCurrentUser().getUserId());
            entity.setLastModifyTime(new Date());
            list.add(entity);
        }
        return tabCoreFunDao.delete(list);

    }

    public List<TabCoreFunEntity> selectByParentIds(Integer[] funIds){
        return tabCoreFunDao.selectByParentIds(funIds);
    }

    public List<TabCoreFunEntity> getFunByUserId(Integer orgId,Integer usserId){
        return tabCoreFunDao.findFunById(orgId,usserId);
    }
}
